package com.heroCW.bodhiAttic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heroCW.bodhiAttic.exception.CustomException;
import com.heroCW.bodhiAttic.entity.Category;
import com.heroCW.bodhiAttic.entity.Dish;
import com.heroCW.bodhiAttic.entity.Setmeal;
import com.heroCW.bodhiAttic.mapper.CategoryMapper;
import com.heroCW.bodhiAttic.resultUtil.ResultCode;
import com.heroCW.bodhiAttic.service.CategoryService;
import com.heroCW.bodhiAttic.service.DishService;
import com.heroCW.bodhiAttic.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {

    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;

    /**
     * 根据ID删除分类，删除之前需要进行判断
     *
     * @param id
     */
    @Override
    @Transactional
    public Boolean remove(Long id) {
        LambdaQueryWrapper<Dish> dishLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dishLambdaQueryWrapper.eq(Dish::getCategoryId, id);
        int count1 = dishService.count(dishLambdaQueryWrapper);

        if (count1 > 0) {
            //已经关联菜品
            throw new CustomException(ResultCode.CUSTOM_CANNOT_DELETE_ERR, "当前分类项关联了菜品，不能删除");
        }
        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, id);
        int count2 = setmealService.count(setmealLambdaQueryWrapper);

        if (count2 > 0) {
            // 已经关联套餐
            throw new CustomException(ResultCode.CUSTOM_CANNOT_DELETE_ERR, "当前分类项关联了套餐，不能删除");
        }
        return super.removeById(id);
    }

    @Override
    public IPage<Category> getPage(int page, int pageSize) {
        // 构造条件构造器
        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
        // 添加排序条件，根据sort进行排序
        categoryWrapper.orderByDesc(Category::getType)
                .orderByAsc(Category::getSort)
                .orderByDesc(Category::getUpdateTime);

        //构造分页构造器
        IPage<Category> pageInfo = new Page<>(page, pageSize);

        return this.page(pageInfo, categoryWrapper);
    }

    @Override
    public List<Category> getByType(Integer type) {

        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
        // 添加条件
        categoryWrapper.eq(type != null, Category::getType, type);
        // 添加排序条件
        categoryWrapper.orderByDesc(Category::getType)
                .orderByAsc(Category::getSort);

        return this.list(categoryWrapper);
    }

    /**
     * 根据名字模糊匹配查询所有ID
     *
     * @param name
     * @return
     */
    @Override
    public List<Long> getIdsLikeName(String name) {

        // SQL: select id where Category.name like %{name}%
        LambdaQueryWrapper<Category> categoryWrapper = new LambdaQueryWrapper<>();
        // 添加条件：分类条目模糊匹配
        categoryWrapper.like(StringUtils.isNotEmpty(name), Category::getName, name);
        // 添加条件：只查询ID
        categoryWrapper.select(Category::getId);

        // 将查询结果通过Lambda表达式放进长整数列表里
        return this.listObjs(categoryWrapper, s -> Long.parseLong(s.toString()));
    }

}
